package cn.yuhh.serviec.impl;

import cn.yuhh.dao.BlogDao;
import cn.yuhh.entity.Blog;
import cn.yuhh.serviec.IBlogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class BlogService implements IBlogService {

    @Autowired
    private BlogDao blogDao;


    @Override
    public Blog getOne(Long id) {
        Optional<Blog> byId = blogDao.findById(id);
        Blog blog = byId.get();
        System.out.println(blog.getId());
        return blog;
    }


    @Override
    public Blog save(Blog blog) {
        blog.setUpdateTime(new Date());
        blog.setCreateTime(new Date());
        return blogDao.save(blog);
    }

    @Override
    public void deleteById(Long id) {
        blogDao.deleteById(id);
    }

    @Override
    public List<Blog> findByTag(Long tagId) {
        return blogDao.findBlogsByTag(tagId);
    }

    @Override
    public List<Blog> findByType(Long typeId) {
        return blogDao.findBlogsByType(typeId);
    }

    @Override
    public Page<Blog> findAll(Pageable pageable) {
        return blogDao.findAll(pageable);
    }

    @Override
    public Blog updataBlog(Long id, Blog blog) {
        Optional<Blog> byId = blogDao.findById(id);
        Blog blog1 = byId.get();
        BeanUtils.copyProperties(blog, blog1);
        blog1.setUpdateTime(new Date());
        return blogDao.save(blog1);
    }
}
